<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit attrvalues.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Attrvalues_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["attrvalue"] = new Attrvalue($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["attrvalue"]->id);

    // To one relationships
        
    $attributes = new Attribute();
    $data["attributes"] = $attributes->all_to_asoc_array("id","name");
    $data["attributes_selected"] = $data["attrvalue"]->attribute->id;
        
    $recipients = new Recipient();
    $data["recipients"] = $recipients->all_to_asoc_array("id","email");
    $data["recipients_selected"] = $data["attrvalue"]->recipient->id;
    
    // To many relationships
    
    $this->smarty_parser->view('attrvalue_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["attrvalue"] = new Attrvalue($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["attrvalue"]->value = $this->input->post("value");
      if ($data["attrvalue"]->value == "") $data["attrvalue"]->value = NULL;
      
      // files for upload
      /*
      if ($data["attrvalue"]->value->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(value)) $data["attrvalue"]->value = $fileupload.clean_file_name(value);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["attrvalue"]->save())
      $data["error"] .= $data["attrvalue"]->error->string;

    //saving to one relationships
    
    $attribute = new Attribute($this->input->post("attribute"));
    if (! $data["attrvalue"]->save(array($attribute)))
      $data["error"] .= $data["attrvalue"]->error->string;
    
    $recipient = new Recipient($this->input->post("recipient"));
    if (! $data["attrvalue"]->save(array($recipient)))
      $data["error"] .= $data["attrvalue"]->error->string;
    
    //to many relationships
    
    redirect(base_url()."attrvalues/edit/".$data["attrvalue"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["attrvalue"] = new Attrvalue($id);

    //delete
    if(! $data["attrvalue"]->delete())
      $data["error"] .= $data["attrvalue"]->error->string;

    redirect(base_url()."attrvalues/edit/");
  } // end of method delete($id)

    
} // class Attrvalues_protoweb

/* End of file attrvalues_protoweb.php */
/* Location: ./system/application/controllers/attrvalues_protoweb.php */
